
#pragma once

#include "zen_vap.h"
#include "MySprite.h"
#include <iostream>
#include <iomanip>
using namespace std;

using namespace Zen;
using namespace Zen::Vap;

class ParLayer : public Layer2d
{
	Random r;
public:
	ParLayer()
	{
	}

	void load() override
	{
		r.reset((uint32_t)time(0));
		auto size = gConfig.size;
		this->addPar1(0, 0);
		this->addPar2(0, 200);
		this->setX(size.w/2);
		this->setRotation(-0.1f);
		this->runAction(ActionRotate2d::Create(Ptr(this), 10000, 0.4));

		this->addFigurePoint();
	}
	void addPar1(float x, float y)
	{
		float sz = 100;
		auto p = std::make_shared<ParticlesGravity>();
		this->addNode(p);

		p->setTexture(TexturesCache::Get()->getTexture("res/o.png"));
//		p->setParticleEmitSpeed(100);
//		p->setParticleLife(1);
//		p->setParticleStartSize(sz);
//		p->setPosition(x, y);
//		p->setOrder(10);
		p->start();
//		p->setPointSizeScale(4.f);

		auto sp = std::make_shared<Sprite>();
		this->addNode(sp);

		sp->setAnchor(0.5f, 0.5f);
		sp->setTexture(TexturesCache::Get()->getTexture("res/o.png"));
		sp->setPosition(x, y - 100);
		sp->setFixedSize({sz, sz});
		sp->enableFixedSize(true);
	}
	void addPar2(float x, float y)
	{
		auto ga = std::make_shared<ParticlesGalaxy>();
		this->addNode(ga);

		ga->setPosition(x, y);
//		ga->setUpdateInterval(0.05f);
//		ga->setRadiusLerpFunction([](float s, float e, float v) {
//			if(s < e) return Zen::Lerp(s, e, v*v);
//			else return Zen::Lerp(e, s, Zen::NumSquare(1-v));
//		});
//		ga->setParticleStartColorsVarying(eColor::White);
//		ga->setParticleEndColorsVarying(eColor::White);
//		ga->setParticleEndRadius(400);
//		ga->setColor(eColor::White);
//		ga->setPosition(x, y);
//		ga->setParticleTotalCount(1128);
		ga->start();


	}
	void addFigurePoint()
	{
		auto fg = std::make_shared<FigurePoints>();
		this->addNode(fg);

		fg->setBlend(eBlend::Add);

		auto fp_content = std::make_shared<FigurePointsContent>();

		fg->setFigureContent(fp_content);

		for(int i = 0; i < 100; ++i)
		{
			auto dot = FigurePointsContent::VertexData();
			dot.coord = { r.nextf() , r.nextf() };
			dot.color = eColor::Grey;
			dot.point_size = 50.f * r.nextf() + 30.f;
			fp_content->addVertex(dot);
		}
		fg->setTexture(TextureLoader::Get()->getParticleTexture());
		fg->setPosition(0, 0);

		fg->runAction(ActionCall::Create([fg,this](float,int) {
			auto & r = gConfig.ra;
			auto fi = fg->getFigureContent();
			auto n = fi->getVertexesCount();
			auto vs = Activity::Get()->getRealViewSize();

			for(size_t i = 0; i < n; ++i)
			{
				auto d=  fi->getVertex(i);
				d.point_size += (0.5 - r.nextf()) * 5;
				if(d.point_size < -50) d.point_size = 50;
				else if(d.point_size > 50) d.point_size = -50;

				d.coord.x += (0.5 - r.nextf()) * 5 / vs.w;
				d.coord.y += (0.5 - r.nextf()) * 5 / vs.h;
				if(d.coord.x > 1) d.coord.x = 1;
				else if(d.coord.x < 0) d.coord.x = 0;
				if(d.coord.y > 1) d.coord.y = 1;
				else if(d.coord.y < 0) d.coord.y = 0;

				d.color.red += (0.5 - r.nextf()) * 0.01f;
				d.color.green += (0.5 - r.nextf()) * 0.01f;
				d.color.blue += (0.5 - r.nextf()) * 0.01f;
				fi->updateVertex(i, d);
			}
			return true;
		}, -1));

	}
	virtual void draw() override
	{
		Layer2d::draw();
	}
};
